//
//  ZDJingXuanViewController.m
//  1012GiftTips
//
//  Created by Apple on 15/10/15.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDJingXuanViewController.h"
#import "ZDJingXuanTopHeadView.h"
#import "Masonry.h"
#import "ZDJumpViewController.h"
#import "ZDTableHeaderView.h"


#define ZDTopHeadViewHight 200

@interface ZDJingXuanViewController ()

@property (nonatomic, strong) ZDJingXuanTopHeadView *topHeadView;

@end

@implementation ZDJingXuanViewController

/**
 *  重写构造方法，设置分组
 */
- (instancetype)init
{
    return  [self initWithStyle:UITableViewStyleGrouped];
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return  [super initWithStyle:UITableViewStyleGrouped];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(ZDTopHeadViewHight, 0, 0, 0);
    
    [self.tableView addSubview:self.topHeadView];
    
    [self.topHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(-ZDTopHeadViewHight);
        make.left.width.mas_equalTo(self.tableView);
        make.height.mas_equalTo(ZDTopHeadViewHight);
    }];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //viewWillAppear里面拿不到控制器view的正确frame，还没有布局好
}


//- (void)viewDidLayoutSubviews     //该方法会执行多次  在该方法中可以拿到控制器view的正确frame
//{
//    ZDLog(@"viewDidLayoutSubviews");
//    
//    self.topHeadView.frame = CGRectMake(0, -self.topHeadView.bounds.size.height, self.view.bounds.size.width, self.topHeadView.bounds.size.height);
//    
//    [self.collectionView addSubview:self.topHeadView];
//}



#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger number;
    
    switch (section)
    {
        case 0:
            number = 2;
            break;
        case 1:
            number = 3;
            break;
        case 2:
            number = 4;
            break;
        case 3:
            number = 5;
            break;
        default:
            break;
    }
    
    return number;
}

//设置组头的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [ZDTableHeaderView tableHeaderView:tableView];
}


//设置组头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ZDTableViewHeaderViewHight;
}


#pragma mark 懒加载
- (ZDJingXuanTopHeadView *)topHeadView
{
    if (!_topHeadView)
    {
        _topHeadView = [ZDJingXuanTopHeadView jingXuanTopHeadView];
        
        __weak typeof(self) weakSelf = self;
        
        _topHeadView.jingPinBtnClickBlock = ^(NSInteger index){
        
            ZDLog(@"点击了产品推荐按钮");
            
            //根据index，取出对应的模型，跳转控制器，显示网页
            ZDJumpViewController *jumpVc = [[ZDJumpViewController alloc] init];
            
            [weakSelf.navigationController pushViewController:jumpVc animated:YES];
        };
    }
    
    return _topHeadView;
}

@end
